package wisents.api;

import wisents.api.factory.ElementCriteriaFactory;
import wisents.api.factory.Vendor;

import java.util.ArrayList;
import java.util.List;

public final class Criterias implements State {

    private final ElementCriteriaFactory criteriaFactory;
    private final List<ElementCriteria> criterias = new ArrayList<ElementCriteria>();
    private final PageElement pageElement;

    public Criterias(PageElement pageElement) {
        this.criteriaFactory = Vendor.getInstance()
                .createElementCriteriaFactory();
        this.pageElement = pageElement;
    }

    public final void assertCriterias() {

        if (criterias.isEmpty()) {
            throw new IllegalStateException("No criterias defined");
        }

        for (ElementCriteria criteria : criterias) {
            criteria.validateCriteria();
        }
    }

    public final Criterias is(ElementCriteria criteria) {
        return addCriteria(criteria);
    }

    public final Criterias exists() {
        return addCriteria(criteriaFactory.createExistCriteria(pageElement));
    }

    public final Criterias isVisible() {
        return addCriteria(criteriaFactory.createIsVisibleCriteria(pageElement));
    }

    public final Criterias isInvisible() {
        return addCriteria(criteriaFactory
                .createIsInvisibleCriteria(pageElement));
    }

    public final Criterias isEnabled() {
        return addCriteria(criteriaFactory.createIsEnabledCriteria(pageElement));
    }

    public final Criterias isDisabled() {
        return addCriteria(criteriaFactory
                .createIsDisabledCriteria(pageElement));
    }

    public final Criterias isTextFieldWithValue(String value) {
        return addCriteria(criteriaFactory.isTextFieldWithValue(pageElement,
                value));
    }

    public final Criterias isTextAreaWithValue(String value) {
        return addCriteria(criteriaFactory.isTextAreaWithValue(pageElement,
                value));
    }

    public final Criterias isChecked() {
        return addCriteria(criteriaFactory.createIsCheckedCriteria(pageElement));
    }

    public final Criterias isNotChecked() {
        return addCriteria(criteriaFactory.createIsNotCheckedCriteria(pageElement));
    }

    public final Criterias isComboboxWithValues(List<?> elements) {
        return addCriteria(criteriaFactory.createIsComboboxWithValuesCriteria(pageElement, elements));
    }

    public final Criterias isLabelWithValue(String value) {
        return addCriteria(criteriaFactory.createIsLabelWithValueCriteria(pageElement, value));
    }

    private Criterias addCriteria(ElementCriteria criteria) {

        if (criteria == null) {
            throw new IllegalStateException("ElementCriteria parameter cannot be null");
        }

        criterias.add(criteria);
        return this;
    }


}
